package com.ghp.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import static com.ghp.common.constants.SystemConstants.SORT_ORDER_ASC;
import static com.ghp.common.constants.SystemConstants.SORT_ORDER_DESC;

/**
 * @author ghp
 * @title MyBatisPlus自定义查询条件构造器
 * @description
 */
public class LambdaQueryWrapperUtils<T> extends LambdaQueryWrapper<T> {

    /**
     * 添加排序条件
     */
    /**
     * 根据字段
     *
     * @param sortField
     * @param sortOrder
     * @return
     */

    public LambdaQueryWrapper<T> addSortCondition(String sortField, String sortOrder) {
        if (!SqlUtils.validSortField(sortField)) {
            // 排序字段发生了SQL注入
            return this;
        }
        return sortOrder.equals(SORT_ORDER_ASC) ?
                this.last("order by " + sortField + SORT_ORDER_ASC) :
                this.last("order by " + sortField + SORT_ORDER_DESC);
    }

}
